import { AccManageConst } from "src/consts/acc_manage_const";
import * as Api from 'src/api/net';
import { PopupName } from "src/consts/popup_const";

const state: ListMsgData = {} as any;
// StateIniter.init(state, 'demo');

state.curr_page = 1;
state.page_size = 15;
state.total_page = 1;
state.total_size = 1;
state.data_list = [];
state.type = "";
// vuex

/** 更新数据的 */
const mutations = {
    [AccManageConst.CommitList](state: ListMsgData, data: ListMsgData): void {
        console.log("acc data update", data);
        state.curr_page = data.curr_page;
        state.page_size = data.page_size;
        state.total_page = data.total_page;
        state.total_size = data.total_size;
        let len = data.data_list.length;
        for (let i = 0; i < len; i++) {
            let tmp: DemoData = data.data_list[i];
            tmp.selected = false;
        }
        state.data_list = data.data_list;
    },
    [AccManageConst.CommitType](state: ListMsgData, data: String): void {
        state.type = data
    }
}

//异步处理
const actions = {
    [AccManageConst.DispatchList](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/account/list.do", data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                WLCommit(AccManageConst.CommitList, res.data);
            }
        })
    },
    [AccManageConst.DispatchQiYong](context: VuexData, data?: any): void {
        let sendData: any = {};
        sendData["id"] = data.id
        sendData["status"] = data.status
        Api.AjaxPost(Config.ajaxUrl + "/account/status/edit.do", sendData, (res: any) => {
            if (data.flag) {
                if (data.type == "qy") {
                    WLMessage("账号启用成功", "success")
                } else if (data.type == "ty") {
                    WLMessage("账号停用成功", "success")
                }
                let senData: any = {}
                senData["curr_page"] = state.curr_page
                senData["page_size"] = state.page_size
                WLDispatch(AccManageConst.DispatchList, senData);
            }
        })
    },
    [AccManageConst.DispatchReset](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/account/password/reset.do", data, (res: any) => {
            if (data.flag) {
                WLMessage("重置密码成功", "success")
                let senData: any = {}
                senData["curr_page"] = state.curr_page
                senData["page_size"] = state.page_size
                WLDispatch(AccManageConst.DispatchList, senData);
            }

        })
    },
}


export default {
    state,
    mutations,
    actions
}